<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 广告模型
 */
class Advertisement extends ModelBase
{
    // 广告位置常量
    const POSITION_BANNER = 'banner';      // 横幅广告
    const POSITION_POPUP = 'popup';        // 弹窗广告
    const POSITION_SIDEBAR = 'sidebar';    // 侧边栏广告
    
    // 状态常量
    const STATUS_DISABLED = 0;  // 禁用
    const STATUS_ENABLED = 1;   // 启用
    
    /**
     * 获取广告位置选项
     */
    public static function getPositionOptions()
    {
        return [
            self::POSITION_BANNER => '横幅广告',
            self::POSITION_POPUP => '弹窗广告',
            self::POSITION_SIDEBAR => '侧边栏广告',
        ];
    }
    
    /**
     * 获取状态选项
     */
    public static function getStatusOptions()
    {
        return [
            self::STATUS_DISABLED => '禁用',
            self::STATUS_ENABLED => '启用',
        ];
    }
    
    /**
     * 获取广告位置文本
     */
    public function getPositionTextAttr($value = null, $data = [])
    {
        $options = self::getPositionOptions();
        return $options[$data['position']] ?? '未知';
    }
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttr($value = null, $data = [])
    {
        $options = self::getStatusOptions();
        return $options[$data['status']] ?? '未知';
    }
    
    /**
     * 获取有效的广告列表
     * @param string $position 广告位置
     * @param int $limit 限制数量
     * @return array
     */
    public function getValidAdvertisementList($position = '', $limit = 0)
    {
        $where = ['status' => self::STATUS_ENABLED];
        
        // 添加位置条件
        if (!empty($position)) {
            $where['position'] = $position;
        }
        
        // 添加时间范围条件
        $now = date('Y-m-d H:i:s');
        $where[] = ['start_time', '<=', $now];
        $where[] = ['end_time', '>=', $now];
        
        $field = 'id,title,content,image_url,link_url,position,sort';
        $order = 'sort DESC, id DESC';
        
        if ($limit > 0) {
            $paginate = ['list_rows' => $limit];
            $result = $this->getList($where, $field, $order, $paginate);
            return $result ?? [];
        } else {
            return $this->getList($where, $field, $order, false);
        }
    }
    
    /**
     * 获取首页横幅广告
     * @param int $limit 限制数量，默认5条
     * @return array
     */
    public function getHomeBannerList($limit = 5)
    {
        return $this->getValidAdvertisementList(self::POSITION_BANNER, $limit);
    }
    
    /**
     * 获取弹窗广告
     * @return array|null
     */
    public function getPopupAdvertisement()
    {
        $list = $this->getValidAdvertisementList(self::POSITION_POPUP, 1);
        return !empty($list) ? $list[0] : null;
    }
    
    /**
     * 检查广告是否有效
     * @param array $advertisement 广告数据
     * @return bool
     */
    public function isValidAdvertisement($advertisement)
    {
        if (empty($advertisement) || $advertisement['status'] != self::STATUS_ENABLED) {
            return false;
        }
        
        $now = time();
        $startTime = strtotime($advertisement['start_time']);
        $endTime = strtotime($advertisement['end_time']);
        
        return $startTime <= $now && $endTime >= $now;
    }
    
    /**
     * 更新广告排序
     * @param int $id 广告ID
     * @param int $sortOrder 排序值
     * @return bool
     */
    public function updateSortOrder($id, $sortOrder)
    {
        return $this->updateInfo(['id' => $id], ['sort' => $sortOrder]);
    }
    
    /**
     * 切换广告状态
     * @param int $id 广告ID
     * @return bool
     */
    public function toggleStatus($id)
    {
        $info = $this->getInfo(['id' => $id], 'status');
        if (empty($info)) {
            return false;
        }
        
        $newStatus = $info['status'] == self::STATUS_ENABLED ? self::STATUS_DISABLED : self::STATUS_ENABLED;
        return $this->updateInfo(['id' => $id], ['status' => $newStatus]);
    }
}